<!DOCTYPE html>
<html lang=en>
<head>
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="description" content="分布式系统随着网站规模的不断扩大，业务的不断发展，系统内部的应用越来越多，常规的垂直应用架构已经无法应对复杂业务带来的各种挑战，传统垂直架构改造的核心就是要对应用进行服务化，实现高内聚低耦合，服务化改造使用的核心技术就是分布式服务框架。那么什么是分布式系统？分布式系统是若干独立计算机的集合，这些计算机对于用户来说就像单个相关系统。分布式系统是建立在网络之上的软件系统。 RPCRPC指的是远程过程调">
<meta name="keywords" content="Java,SpringBoot,Zookeeper,Dubbo">
<meta property="og:type" content="article">
<meta property="og:title" content="SpringBoot Zookeeper Dubbo简单搭建分布式系统">
<meta property="og:url" content="http:&#x2F;&#x2F;yoursite.com&#x2F;2019&#x2F;11&#x2F;17&#x2F;SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F&#x2F;index.html">
<meta property="og:site_name" content="ECIN&#39;S BLOG">
<meta property="og:description" content="分布式系统随着网站规模的不断扩大，业务的不断发展，系统内部的应用越来越多，常规的垂直应用架构已经无法应对复杂业务带来的各种挑战，传统垂直架构改造的核心就是要对应用进行服务化，实现高内聚低耦合，服务化改造使用的核心技术就是分布式服务框架。那么什么是分布式系统？分布式系统是若干独立计算机的集合，这些计算机对于用户来说就像单个相关系统。分布式系统是建立在网络之上的软件系统。 RPCRPC指的是远程过程调">
<meta property="og:locale" content="en">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1573990439057.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1573990681272.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1573991018666.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1573991206885.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1573992148992.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1573992186769.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1573992406093.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1573992473889.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1573992501654.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1573992582226.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1573993726152.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1573993771567.jpg">
<meta property="og:updated_time" content="2019-11-17T12:35:51.580Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1573990439057.jpg">
    
    
        
          
              <link rel="shortcut icon" href="/images/favicon.ico">
          
        
        
          
            <link rel="icon" type="image/png" href="/images/favicon-192x192.png" sizes="192x192">
          
        
        
          
            <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
          
        
    
    <!-- title -->
    <title>SpringBoot Zookeeper Dubbo简单搭建分布式系统</title>
    <!-- styles -->
    <link rel="stylesheet" href="/css/style.css">
    <!-- persian styles -->
    
      <link rel="stylesheet" href="/css/rtl.css">
    
    <!-- rss -->
    
    
</head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" href="/2019/11/19/Netty%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E7%9E%8E%E6%90%9E/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" href="/2019/11/14/WebSocket/"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">Previous post</span>
      <span id="i-next" class="info" style="display:none;">Next post</span>
      <span id="i-top" class="info" style="display:none;">Back to top</span>
      <span id="i-share" class="info" style="display:none;">Share post</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=http://yoursite.com/2019/11/17/SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/" target="_blank" rel="noopener"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=http://yoursite.com/2019/11/17/SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/&text=SpringBoot Zookeeper Dubbo简单搭建分布式系统" target="_blank" rel="noopener"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=http://yoursite.com/2019/11/17/SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/&title=SpringBoot Zookeeper Dubbo简单搭建分布式系统" target="_blank" rel="noopener"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=http://yoursite.com/2019/11/17/SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/&is_video=false&description=SpringBoot Zookeeper Dubbo简单搭建分布式系统" target="_blank" rel="noopener"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=SpringBoot Zookeeper Dubbo简单搭建分布式系统&body=Check out this article: http://yoursite.com/2019/11/17/SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/" target="_blank" rel="noopener"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=http://yoursite.com/2019/11/17/SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/&title=SpringBoot Zookeeper Dubbo简单搭建分布式系统" target="_blank" rel="noopener"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=http://yoursite.com/2019/11/17/SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/&title=SpringBoot Zookeeper Dubbo简单搭建分布式系统" target="_blank" rel="noopener"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=http://yoursite.com/2019/11/17/SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/&title=SpringBoot Zookeeper Dubbo简单搭建分布式系统" target="_blank" rel="noopener"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=http://yoursite.com/2019/11/17/SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/&title=SpringBoot Zookeeper Dubbo简单搭建分布式系统" target="_blank" rel="noopener"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=http://yoursite.com/2019/11/17/SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/&name=SpringBoot Zookeeper Dubbo简单搭建分布式系统&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=http://yoursite.com/2019/11/17/SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/&t=SpringBoot Zookeeper Dubbo简单搭建分布式系统" target="_blank" rel="noopener"><i class="fab fa-hacker-news " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#分布式系统"><span class="toc-number">1.</span> <span class="toc-text">分布式系统</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#RPC"><span class="toc-number">2.</span> <span class="toc-text">RPC</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#SpringCloud与Dubbo"><span class="toc-number">3.</span> <span class="toc-text">SpringCloud与Dubbo</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Dubbo"><span class="toc-number">4.</span> <span class="toc-text">Dubbo</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#基本概念"><span class="toc-number">4.1.</span> <span class="toc-text">基本概念</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#调用关系"><span class="toc-number">4.2.</span> <span class="toc-text">调用关系</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Zookeeper"><span class="toc-number">5.</span> <span class="toc-text">Zookeeper</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#SpringBoot整合Zookeeper与Dubbo"><span class="toc-number">6.</span> <span class="toc-text">SpringBoot整合Zookeeper与Dubbo</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#服务提供者-provider"><span class="toc-number">6.1.</span> <span class="toc-text">服务提供者 provider</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#在pom-xml中添加依赖"><span class="toc-number">6.1.1.</span> <span class="toc-text">在pom.xml中添加依赖</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#yml配置服务"><span class="toc-number">6.1.2.</span> <span class="toc-text">yml配置服务</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#编写服务"><span class="toc-number">6.1.3.</span> <span class="toc-text">编写服务</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#消费者-consumer"><span class="toc-number">6.2.</span> <span class="toc-text">消费者 consumer</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#在pom-xml中添加依赖，和provider一样"><span class="toc-number">6.2.1.</span> <span class="toc-text">在pom.xml中添加依赖，和provider一样</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#yml配置"><span class="toc-number">6.2.2.</span> <span class="toc-text">yml配置</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#消费者使用提供者的服务正常步骤一般为打包provider成jar后，然后在consumer的pom中添加依赖，提供给consumer使用。但是为了方便使用，利用一个更简单的方法，在consumer中添加和provider相同的包（路径和名称必须相同）。这样才会不报错。"><span class="toc-number">6.2.3.</span> <span class="toc-text">消费者使用提供者的服务正常步骤一般为打包provider成jar后，然后在consumer的pom中添加依赖，提供给consumer使用。但是为了方便使用，利用一个更简单的方法，在consumer中添加和provider相同的包（路径和名称必须相同）。这样才会不报错。</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#开始执行程序"><span class="toc-number">6.3.</span> <span class="toc-text">开始执行程序</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#打开Zookeeper注册中心"><span class="toc-number">6.3.1.</span> <span class="toc-text">打开Zookeeper注册中心</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#运行提供者"><span class="toc-number">6.3.2.</span> <span class="toc-text">运行提供者</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#运行消费者"><span class="toc-number">6.3.3.</span> <span class="toc-text">运行消费者</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#访问接口"><span class="toc-number">6.3.4.</span> <span class="toc-text">访问接口</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#dubbo-admin查看zookeeper状态"><span class="toc-number">7.</span> <span class="toc-text">dubbo-admin查看zookeeper状态</span></a></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        SpringBoot Zookeeper Dubbo简单搭建分布式系统
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">ECIN'S BLOG</span>
      </span>
      
    <div class="postdate">
      
        <time datetime="2019-11-17T12:34:54.000Z" itemprop="datePublished">2019-11-17</time>
        
      
    </div>


      

      
    <div class="article-tag">
        <i class="fas fa-tag"></i>
        <a class="tag-link" href="/tags/Dubbo/" rel="tag">Dubbo</a>, <a class="tag-link" href="/tags/Java/" rel="tag">Java</a>, <a class="tag-link" href="/tags/SpringBoot/" rel="tag">SpringBoot</a>, <a class="tag-link" href="/tags/Zookeeper/" rel="tag">Zookeeper</a>
    </div>


    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <h4 id="分布式系统"><a href="#分布式系统" class="headerlink" title="分布式系统"></a>分布式系统</h4><p>随着网站规模的不断扩大，业务的不断发展，系统内部的应用越来越多，常规的垂直应用架构已经无法应对复杂业务带来的各种挑战，传统垂直架构改造的核心就是要对应用进行服务化，实现高内聚低耦合，服务化改造使用的核心技术就是分布式服务框架。那么什么是分布式系统？<br>分布式系统是若干独立计算机的集合，这些计算机对于用户来说就像单个相关系统。<br>分布式系统是建立在网络之上的软件系统。</p>
<h4 id="RPC"><a href="#RPC" class="headerlink" title="RPC"></a>RPC</h4><p>RPC指的是远程过程调用，是一种进程间通信的方式，是一种技术思想，它允许调用另一台主机的服务，开发人员却不用关心底层的通信细节。</p>
<h4 id="SpringCloud与Dubbo"><a href="#SpringCloud与Dubbo" class="headerlink" title="SpringCloud与Dubbo"></a>SpringCloud与Dubbo</h4><p>SpringCloud拥有强大的生态，Dubbo专注于RPC，在RPC这一块，使用Dubbo较佳。</p>
<h4 id="Dubbo"><a href="#Dubbo" class="headerlink" title="Dubbo"></a>Dubbo</h4><p>Dubbo是一款高性能，轻量级的开源JAVA RPC框架，它提供了三大核心能力：</p>
<ul>
<li>面向接口的远程方法调用</li>
<li>智能容错和负载均衡</li>
<li>服务自动注册和发现。</li>
</ul>
<p><a href="http://dubbo.apache.org/zh-cn/index.html" target="_blank" rel="noopener">Dubbo官网</a></p>
<h5 id="基本概念"><a href="#基本概念" class="headerlink" title="基本概念"></a>基本概念</h5><p><img src="http://148.70.50.70/source/1573990439057.jpg" alt="image.png"></p>
<ul>
<li><p>服务提供者（Provider）：暴露服务的服务提供方，服务提供者在启动时，向注册中心注册自己提供的服务。</p>
</li>
<li><p>服务消费者（Consumer）: 调用远程服务的服务消费方，服务消费者在启动时，向注册中心订阅自己所需的服务，服务消费者，从提供者地址列表中，基于软负载均衡算法，选一台提供者进行调用，如果调用失败，再选另一台调用。</p>
</li>
<li><p>注册中心（Registry）：注册中心返回服务提供者地址列表给消费者，如果有变更，注册中心将基于长连接推送变更数据给消费者</p>
</li>
<li><p>监控中心（Monitor）：服务消费者和提供者，在内存中累计调用次数和调用时间，定时每分钟发送一次统计数据到监控中心</p>
</li>
</ul>
<h5 id="调用关系"><a href="#调用关系" class="headerlink" title="调用关系"></a>调用关系</h5><ul>
<li><p>服务容器负责启动，加载，运行服务提供者。</p>
</li>
<li><p>服务提供者在启动时，向注册中心注册自己提供的服务。</p>
</li>
<li><p>服务消费者在启动时，向注册中心订阅自己所需的服务。</p>
</li>
<li><p>注册中心返回服务提供者地址列表给消费者，如果有变更，注册中心将基于长连接推送变更数据给消费者。</p>
</li>
<li><p>服务消费者，从提供者地址列表中，基于软负载均衡算法，选一台提供者进行调用，如果调用失败，再选另一台调用。</p>
</li>
<li><p>服务消费者和提供者，在内存中累计调用次数和调用时间，定时每分钟发送一次统计数据到监控中心。</p>
<h4 id="Zookeeper"><a href="#Zookeeper" class="headerlink" title="Zookeeper"></a>Zookeeper</h4><p>Zookeeper是Apacahe Hadoop 的子项目，是一个树型的目录服务，支持变更推送，适合作为 Dubbo 服务的注册中心，工业强度较高，可用于生产环境。</p>
</li>
</ul>
<p><img src="http://148.70.50.70/source/1573990681272.jpg" alt="image.png"></p>
<p><a href="http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.14/" target="_blank" rel="noopener">Zookeeper下载</a><br>windows和Linux都可适用，首先需要将conf/zoo_sample.cfg文件复制为conf/zoo.cfg</p>
<p><img src="http://148.70.50.70/source/1573991018666.jpg" alt="image.png"></p>
<p>然后打开 bin/zkServer.cmd 若不报错则正常运行</p>
<h4 id="SpringBoot整合Zookeeper与Dubbo"><a href="#SpringBoot整合Zookeeper与Dubbo" class="headerlink" title="SpringBoot整合Zookeeper与Dubbo"></a>SpringBoot整合Zookeeper与Dubbo</h4><p>idea创建一个空项目，然后添加两个Module-生产者-消费者</p>
<p><img src="http://148.70.50.70/source/1573991206885.jpg" alt="image.png"></p>
<h5 id="服务提供者-provider"><a href="#服务提供者-provider" class="headerlink" title="服务提供者 provider"></a>服务提供者 provider</h5><h6 id="在pom-xml中添加依赖"><a href="#在pom-xml中添加依赖" class="headerlink" title="在pom.xml中添加依赖"></a>在pom.xml中添加依赖</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- Dubbo Spring Boot Starter --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.dubbo<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>dubbo-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.7.3<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.github.sgroschupf<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>zkclient<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 引入zookeeper --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.curator<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>curator-framework<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.12.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.curator<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>curator-recipes<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.12.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.zookeeper<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>zookeeper<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.4.14<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h6 id="yml配置服务"><a href="#yml配置服务" class="headerlink" title="yml配置服务"></a>yml配置服务</h6><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">server:</span></span><br><span class="line"><span class="attr">  port:</span> <span class="number">8081</span></span><br><span class="line"></span><br><span class="line"><span class="attr">dubbo:</span></span><br><span class="line"><span class="attr">  application:</span></span><br><span class="line">    <span class="comment"># 当前应用的名称</span></span><br><span class="line"><span class="attr">    name:</span> <span class="string">provider</span></span><br><span class="line"><span class="attr">  registry:</span></span><br><span class="line">    <span class="comment"># 注册中心地址</span></span><br><span class="line"><span class="attr">    address:</span> <span class="attr">zookeeper://127.0.0.1:2181</span></span><br><span class="line"><span class="attr">  scan:</span></span><br><span class="line">    <span class="comment"># 扫描包下的服务</span></span><br><span class="line"><span class="attr">    base-packages:</span> <span class="string">com\ecin520\provider\service</span></span><br></pre></td></tr></table></figure>

<h6 id="编写服务"><a href="#编写服务" class="headerlink" title="编写服务"></a>编写服务</h6><p>服务接口</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.ecin520.provider.service;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">TicketService</span> </span>&#123;</span><br><span class="line">    <span class="function">String <span class="title">getTicket</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>实现接口</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.ecin520.provider.service.impl;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.ecin520.provider.service.TicketService;</span><br><span class="line"><span class="keyword">import</span> org.apache.dubbo.config.annotation.Service;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Component;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Service注解为Dubbo</span></span><br><span class="line"><span class="comment"> * 将类放到容器中</span></span><br><span class="line"><span class="comment"> * 程序启动Dubbo会自动扫描指定包下面<span class="doctag">@Component</span>的类，将其发布到注册中心</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TicketServiceImpl</span> <span class="keyword">implements</span> <span class="title">TicketService</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getTicket</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"Ticket remain 5."</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这样就写好了一个提供剩余售票的提供者</p>
<h5 id="消费者-consumer"><a href="#消费者-consumer" class="headerlink" title="消费者 consumer"></a>消费者 consumer</h5><h6 id="在pom-xml中添加依赖，和provider一样"><a href="#在pom-xml中添加依赖，和provider一样" class="headerlink" title="在pom.xml中添加依赖，和provider一样"></a>在pom.xml中添加依赖，和provider一样</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- Dubbo Spring Boot Starter --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.dubbo<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>dubbo-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.7.3<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.github.sgroschupf<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>zkclient<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 引入zookeeper --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.curator<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>curator-framework<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.12.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.curator<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>curator-recipes<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.12.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.zookeeper<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>zookeeper<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.4.14<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h6 id="yml配置"><a href="#yml配置" class="headerlink" title="yml配置"></a>yml配置</h6><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">server:</span></span><br><span class="line"><span class="attr">  port:</span> <span class="number">8082</span></span><br><span class="line"></span><br><span class="line"><span class="attr">dubbo:</span></span><br><span class="line"><span class="attr">  application:</span></span><br><span class="line">    <span class="comment"># 当前应用名称</span></span><br><span class="line"><span class="attr">    name:</span> <span class="string">consumer</span></span><br><span class="line"><span class="attr">  registry:</span></span><br><span class="line">    <span class="comment"># 注册中心地址</span></span><br><span class="line"><span class="attr">    address:</span> <span class="attr">zookeeper://127.0.0.1:2181</span></span><br></pre></td></tr></table></figure>
<h6 id="消费者使用提供者的服务正常步骤一般为打包provider成jar后，然后在consumer的pom中添加依赖，提供给consumer使用。但是为了方便使用，利用一个更简单的方法，在consumer中添加和provider相同的包（路径和名称必须相同）。这样才会不报错。"><a href="#消费者使用提供者的服务正常步骤一般为打包provider成jar后，然后在consumer的pom中添加依赖，提供给consumer使用。但是为了方便使用，利用一个更简单的方法，在consumer中添加和provider相同的包（路径和名称必须相同）。这样才会不报错。" class="headerlink" title="消费者使用提供者的服务正常步骤一般为打包provider成jar后，然后在consumer的pom中添加依赖，提供给consumer使用。但是为了方便使用，利用一个更简单的方法，在consumer中添加和provider相同的包（路径和名称必须相同）。这样才会不报错。"></a>消费者使用提供者的服务正常步骤一般为打包provider成jar后，然后在consumer的pom中添加依赖，提供给consumer使用。但是为了方便使用，利用一个更简单的方法，在consumer中添加和provider相同的包（路径和名称必须相同）。这样才会不报错。</h6><p>提供者</p>
<p><img src="http://148.70.50.70/source/1573992148992.jpg" alt="image.png"></p>
<p>消费者</p>
<p><img src="http://148.70.50.70/source/1573992186769.jpg" alt="image.png"></p>
<p>消费者代码 UserService</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.ecin520.consumer.service;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.ecin520.provider.service.TicketService;</span><br><span class="line"><span class="keyword">import</span> org.apache.dubbo.config.annotation.Reference;</span><br><span class="line"><span class="keyword">import</span> org.apache.dubbo.config.annotation.Service;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Component;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 本地调用用的是<span class="doctag">@Autowire</span></span></span><br><span class="line"><span class="comment">     * 远程调用使用的是Dubbo的<span class="doctag">@Reference</span></span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="meta">@Reference</span></span><br><span class="line">    <span class="keyword">private</span> TicketService ticketService;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">queryTicketCount</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> ticketService.getTicket();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>编写UserController测试</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.ecin520.consumer.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.ecin520.consumer.service.UserService;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RestController;</span><br><span class="line"></span><br><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    UserService userService;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@RequestMapping</span>(<span class="string">"/getTickets"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getTickets</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> userService.queryTicketCount();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="开始执行程序"><a href="#开始执行程序" class="headerlink" title="开始执行程序"></a>开始执行程序</h5><h6 id="打开Zookeeper注册中心"><a href="#打开Zookeeper注册中心" class="headerlink" title="打开Zookeeper注册中心"></a>打开Zookeeper注册中心</h6><p><img src="http://148.70.50.70/source/1573992406093.jpg" alt="image.png"></p>
<h6 id="运行提供者"><a href="#运行提供者" class="headerlink" title="运行提供者"></a>运行提供者</h6><p><img src="http://148.70.50.70/source/1573992473889.jpg" alt="image.png"></p>
<h6 id="运行消费者"><a href="#运行消费者" class="headerlink" title="运行消费者"></a>运行消费者</h6><p><img src="http://148.70.50.70/source/1573992501654.jpg" alt="image.png"></p>
<h6 id="访问接口"><a href="#访问接口" class="headerlink" title="访问接口"></a>访问接口</h6><p><img src="http://148.70.50.70/source/1573992582226.jpg" alt="image.png"></p>
<p>远程调用成功</p>
<h4 id="dubbo-admin查看zookeeper状态"><a href="#dubbo-admin查看zookeeper状态" class="headerlink" title="dubbo-admin查看zookeeper状态"></a>dubbo-admin查看zookeeper状态</h4><p><a href="https://github.com/apache/dubbo-admin" target="_blank" rel="noopener">https://github.com/apache/dubbo-admin</a></p>
<p><img src="http://148.70.50.70/source/1573993726152.jpg" alt="image.png"></p>
<p>修改为zookeeper地址即可，运行后</p>
<p><img src="http://148.70.50.70/source/1573993771567.jpg" alt="image.png"></p>

  </div>
</article>



        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#分布式系统"><span class="toc-number">1.</span> <span class="toc-text">分布式系统</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#RPC"><span class="toc-number">2.</span> <span class="toc-text">RPC</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#SpringCloud与Dubbo"><span class="toc-number">3.</span> <span class="toc-text">SpringCloud与Dubbo</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Dubbo"><span class="toc-number">4.</span> <span class="toc-text">Dubbo</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#基本概念"><span class="toc-number">4.1.</span> <span class="toc-text">基本概念</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#调用关系"><span class="toc-number">4.2.</span> <span class="toc-text">调用关系</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Zookeeper"><span class="toc-number">5.</span> <span class="toc-text">Zookeeper</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#SpringBoot整合Zookeeper与Dubbo"><span class="toc-number">6.</span> <span class="toc-text">SpringBoot整合Zookeeper与Dubbo</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#服务提供者-provider"><span class="toc-number">6.1.</span> <span class="toc-text">服务提供者 provider</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#在pom-xml中添加依赖"><span class="toc-number">6.1.1.</span> <span class="toc-text">在pom.xml中添加依赖</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#yml配置服务"><span class="toc-number">6.1.2.</span> <span class="toc-text">yml配置服务</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#编写服务"><span class="toc-number">6.1.3.</span> <span class="toc-text">编写服务</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#消费者-consumer"><span class="toc-number">6.2.</span> <span class="toc-text">消费者 consumer</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#在pom-xml中添加依赖，和provider一样"><span class="toc-number">6.2.1.</span> <span class="toc-text">在pom.xml中添加依赖，和provider一样</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#yml配置"><span class="toc-number">6.2.2.</span> <span class="toc-text">yml配置</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#消费者使用提供者的服务正常步骤一般为打包provider成jar后，然后在consumer的pom中添加依赖，提供给consumer使用。但是为了方便使用，利用一个更简单的方法，在consumer中添加和provider相同的包（路径和名称必须相同）。这样才会不报错。"><span class="toc-number">6.2.3.</span> <span class="toc-text">消费者使用提供者的服务正常步骤一般为打包provider成jar后，然后在consumer的pom中添加依赖，提供给consumer使用。但是为了方便使用，利用一个更简单的方法，在consumer中添加和provider相同的包（路径和名称必须相同）。这样才会不报错。</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#开始执行程序"><span class="toc-number">6.3.</span> <span class="toc-text">开始执行程序</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#打开Zookeeper注册中心"><span class="toc-number">6.3.1.</span> <span class="toc-text">打开Zookeeper注册中心</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#运行提供者"><span class="toc-number">6.3.2.</span> <span class="toc-text">运行提供者</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#运行消费者"><span class="toc-number">6.3.3.</span> <span class="toc-text">运行消费者</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#访问接口"><span class="toc-number">6.3.4.</span> <span class="toc-text">访问接口</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#dubbo-admin查看zookeeper状态"><span class="toc-number">7.</span> <span class="toc-text">dubbo-admin查看zookeeper状态</span></a></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=http://yoursite.com/2019/11/17/SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/" target="_blank" rel="noopener"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=http://yoursite.com/2019/11/17/SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/&text=SpringBoot Zookeeper Dubbo简单搭建分布式系统" target="_blank" rel="noopener"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=http://yoursite.com/2019/11/17/SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/&title=SpringBoot Zookeeper Dubbo简单搭建分布式系统" target="_blank" rel="noopener"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=http://yoursite.com/2019/11/17/SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/&is_video=false&description=SpringBoot Zookeeper Dubbo简单搭建分布式系统" target="_blank" rel="noopener"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=SpringBoot Zookeeper Dubbo简单搭建分布式系统&body=Check out this article: http://yoursite.com/2019/11/17/SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/" target="_blank" rel="noopener"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=http://yoursite.com/2019/11/17/SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/&title=SpringBoot Zookeeper Dubbo简单搭建分布式系统" target="_blank" rel="noopener"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=http://yoursite.com/2019/11/17/SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/&title=SpringBoot Zookeeper Dubbo简单搭建分布式系统" target="_blank" rel="noopener"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=http://yoursite.com/2019/11/17/SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/&title=SpringBoot Zookeeper Dubbo简单搭建分布式系统" target="_blank" rel="noopener"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=http://yoursite.com/2019/11/17/SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/&title=SpringBoot Zookeeper Dubbo简单搭建分布式系统" target="_blank" rel="noopener"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=http://yoursite.com/2019/11/17/SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/&name=SpringBoot Zookeeper Dubbo简单搭建分布式系统&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=http://yoursite.com/2019/11/17/SpringBoot-Zookeeper-Dubbo%E7%AE%80%E5%8D%95%E6%90%AD%E5%BB%BA%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/&t=SpringBoot Zookeeper Dubbo简单搭建分布式系统" target="_blank" rel="noopener"><i class="fab fa-hacker-news fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> Menu</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> TOC</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> Share</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> Top</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy; 2020 ECIN520
  </div>
  <div class="footer-right">
    <nav>
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </nav>
  </div>
</footer>

    </div>
    <!-- styles -->
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
<link rel="stylesheet" href="/lib/justified-gallery/css/justifiedGallery.min.css">

    <!-- jquery -->
<script src="/lib/jquery/jquery.min.js"></script>
<script src="/lib/justified-gallery/js/jquery.justifiedGallery.min.js"></script>
<!-- clipboard -->

  <script src="/lib/clipboard/clipboard.min.js"></script>
  <script type="text/javascript">
  $(function() {
    // copy-btn HTML
    var btn = "<span class=\"btn-copy tooltipped tooltipped-sw\" aria-label=\"Copy to clipboard!\">";
    btn += '<i class="far fa-clone"></i>';
    btn += '</span>'; 
    // mount it!
    $(".highlight table").before(btn);
    var clip = new ClipboardJS('.btn-copy', {
      text: function(trigger) {
        return Array.from(trigger.nextElementSibling.querySelectorAll('.code')).reduce((str,it)=>str+it.innerText+'\n','')
      }
    });
    clip.on('success', function(e) {
      e.trigger.setAttribute('aria-label', "Copied!");
      e.clearSelection();
    })
  })
  </script>

<script src="/js/main.js"></script>
<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

<!-- Disqus Comments -->


</body>
</html>
